package com.wufun.overseas.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.wufun.overseas.R;
import com.wufun.overseas.view.lib.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiet on 2015/7/8.
 */
public class MyAlbumAdapter extends BaseAdapter {

    public static final String CAMERA = "camera";
    private List<String> mSelectedImages;
    private String dirPath;

    private Context mContext;
    private List<String> images;

    public MyAlbumAdapter(Context context, List<String> images, String dir, List<String> selectedImages) {
        this.mContext = context;
        this.images = images;
        this.dirPath = dir;
        this.mSelectedImages = selectedImages;

        addCamera();
    }

    private void addCamera() {
        if (images == null)
            images = new ArrayList<String>();
        List<String> imagesT = new ArrayList<String>();
        imagesT.add(CAMERA);
        imagesT.addAll(images);

        images = new ArrayList<String>();
        images.addAll(imagesT);

        imagesT = null;
    }

    @Override
    public int getCount() {
        if (images == null) return 0;
        return images.size();
    }

    @Override
    public Object getItem(int position) {
        return dirPath + File.separator + images.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mContext == null || getCount() == 0) return convertView;
        try {
            ViewHolder holder;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                holder = new ViewHolder();
                View view = LayoutInflater.from(mContext).inflate(R.layout.my_album_item, null);

                holder.image = (ImageView) view.findViewById(R.id.image);
                holder.flag = (ImageView) view.findViewById(R.id.image_flag);

                convertView = view;
                convertView.setTag(holder);
            }

          /*  int itemWidth = 0;
            if (Build.VERSION.SDK_INT >= 16 && false)
                itemWidth = ((GridView) parent).getColumnWidth();//API 16
            else
                itemWidth = (int) (mContext.getResources().getDisplayMetrics().widthPixels / 3 - 15);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
            holder.image.setLayoutParams(params);*/

            //set default
            holder.image.setImageResource(R.drawable.main_normal_icon);
            holder.image.setColorFilter(null);
            holder.flag.setImageResource(R.mipmap.pic_select_default);

            //set images
            String item = (String) getItem(position);
            if (images.get(position).equals(CAMERA)) {
                holder.image.setImageResource(R.drawable.camera);
                holder.flag.setVisibility(View.GONE);
            } else {
                setImageByUrl(holder.image, item);
                holder.flag.setVisibility(View.VISIBLE);

                checkSelected(holder.image, holder.flag, item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private void checkSelected(ImageView image, ImageView flag, String item) {
        if (mSelectedImages == null || mSelectedImages.size() == 0) return;

        for (int i = 0; i < mSelectedImages.size(); i++) {
            if (mSelectedImages.get(i).equals(item)) {
               // image.setColorFilter(Color.parseColor("#7f989898"));
                flag.setImageResource(R.drawable.pic_select_selected);
            }
        }
    }

    @Override
    public void notifyDataSetChanged() {
        addCamera();
        super.notifyDataSetChanged();
    }

    public static ImageLoader imageLoader() {
        return ImageLoader.getInstance(3, ImageLoader.Type.LIFO);
    }

    private void setImageByUrl(ImageView view, String url) {
        imageLoader().loadImage(url, view);
    }

    public class ViewHolder {
        ImageView image;
        ImageView flag;
    }
}
